package nil.ed.simulator.request.ui.jfx.controller;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.Setter;
import nil.ed.simulator.request.enums.events.EventMode;
import nil.ed.simulator.request.enums.events.EventType;
import nil.ed.simulator.request.event.Event;
import nil.ed.simulator.request.event.bus.EventBus;
import nil.ed.simulator.request.guice.GuiceManager;
import nil.ed.simulator.request.ui.jfx.manager.ControllerManager;
import nil.ed.simulator.request.ui.jfx.ui.Controller;

import java.net.URL;
import java.util.ResourceBundle;

public class TopControlPaneController extends VBox implements Controller, Initializable {
	@FXML
	private TextField inputThreadNum;
	@FXML
	private Label showTotalQQ;
	@FXML
	private Button startButton;
	@FXML
	private ProgressBar progress;
	@FXML
	private Label statusLabel;
	@Setter
	@Getter
	private ControllerManager controllerManager;

	private final SimpleDoubleProperty progressProps = new SimpleDoubleProperty(0.0);
	@Override
	public void initialize(URL utl, ResourceBundle source) {
		ControllerManager.addController(this);
		progress.progressProperty().bind(progressProps);
	}

	public void startHandler(ActionEvent e) {
		// 开始
		GuiceManager.INJECTOR.getInstance(EventBus.class)
				.publish(Event.builder()
						.source(null)
						.data(null)
						.eventMode(EventMode.ASYNC)
						.eventType(EventType.START_ACTIVITY)
						.build());
	}

	public void configProgress(int total, int finished) {
		// 总数/完成:0/0
		statusLabel.setText(String.format("总数/完成:%s/%s", total, finished));
		progressProps.set(1.0 * finished / total);
	}

}
